const express = require('express')
const getRouter = express.Router()
const { date_fns, db } = require('../tools')
//获取首页轮播图
getRouter.get('/shouye', (req, res) => {
  const sql = `select * from lunbo order by lunbo_id desc`
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('轮播图获取接口错误', err.message)
    } else {
      res.send(data)
      console.log('轮播图获取接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})
//获取首页每日资讯
getRouter.get('/zixun', (req, res) => {
  const sql = `select * from zixun`
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('每日资讯获取接口错误', err.message)
    } else {
      res.send(data)
      console.log('每日资讯获取接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})
//获取个人信息
getRouter.get('/user', (req, res) => {
  let { username } = req.query
  const sql = `select * from users where username='${username}'`
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('个人信息获取接口错误', err.message)
    } else {
      res.send(data)
      console.log('个人信息获取接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})
//获取领养列表
getRouter.get('/lingyanglist', (req, res) => {
  const sql = `select * from pets order by pet_id desc`
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('领养列表获取接口错误', err.message)
    } else {
      res.send(data)
      console.log('领养列表获取接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})
//获取要领养宠物的详细信息
getRouter.get('/cwxx', (req, res) => {
  let { pet_id } = req.query
  const sql = `select * from pets where pet_id=${pet_id}`
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('要领养宠物的详细信息接口错误', err.message)
    } else {
      res.send(data)
      console.log('要领养宠物的详细信息接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})
//获取待审核宠物的详细信息
getRouter.get('/shenhe', (req, res) => {
  let { username } = req.query
  const sql = `select adopts.adopt_time,adopts.true_name,adopts.phone,adopts.address,adopts.status,adopts.adopt_status,adopts.reason,pets.image,pets.petname from adopts join pets on adopts.pet_id=pets.pet_id where username='${username}' order by adopt_id desc`
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('待审核宠物的详细信息接口错误', err.message)
    } else {
      res.send(data)
      console.log('待审核宠物的详细信息接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})
//获取救助流浪宠物的列表
getRouter.get('/jiuchong', (req, res) => {
  let { username } = req.query
  const sql = `select * from helps where username='${username}' order by help_id desc `
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('用户救助流浪宠物列表接口错误', err.message)
    } else {
      res.send(data)
      console.log('用户救助流浪宠物列表接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})

//获取论坛列表
getRouter.get('/forums', (req, res) => {
  const sql = `select forums.username,forums.forum_id,forums.title,forums.content,forums.publish_time,users.head_img from forums join users on forums.username=users.username  order by forum_id desc `
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('论坛列表接口错误', err.message)
    } else {
      res.send(data)
      console.log('论坛列表接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})
//获取评论列表
getRouter.get('/reviews', (req, res) => {
  let { forum_id } = req.query
  const sql = `select reviews.username,reviews.forum_id,reviews.review,reviews.review_time,reviews.review_id,users.head_img from reviews join users on reviews.username=users.username where forum_id='${forum_id}' order by review_id desc `
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('评论列表接口错误', err.message)
    } else {
      res.send(data)
      console.log('评论列表接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})
//获取养宠小知识列表
getRouter.get('/knowledge', (req, res) => {
  const sql = `select * from knowledge order by knowledge_id desc`
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('养宠小知识列表接口错误', err.message)
    } else {
      res.send(data)
      console.log('养宠小知识列表接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})

/**
 *
 *
 * 管理系统接口
 *
 *
 *
 *
 */
//获取用户信息
getRouter.get('/users', (req, res) => {
  const sql = `select * from users`
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('用户信息获取接口错误', err.message)
    } else {
      res.send(data)
      console.log('用户信息获取接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})
//获取待审核宠物管理接口
getRouter.get('/daishen', (req, res) => {
  const sql = `select adopts.adopt_id,adopts.adopt_time,adopts.true_name,adopts.phone,adopts.address,adopts.status,adopts.adopt_status,adopts.reason,pets.pet_id,pets.image,pets.petname from adopts join pets on adopts.pet_id=pets.pet_id where adopts.status='待审核'`
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('待审核宠物管理接口错误', err.message)
    } else {
      res.send(data)
      console.log('待审核宠物管理接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})
//获取待领养宠物管理接口
getRouter.get('/dailing', (req, res) => {
  const sql = `select adopt_id,adopts.adopt_time,adopts.true_name,adopts.phone,adopts.address,adopts.status,adopts.adopt_status,adopts.reason,pets.pet_id,pets.image,pets.petname from adopts join pets on adopts.pet_id=pets.pet_id where adopts.adopt_status='待领养' order by adopt_id desc`
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('待领养宠物管理接口错误', err.message)
    } else {
      res.send(data)
      console.log('待领养宠物管理接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})
//获取已领养宠物管理接口
getRouter.get('/yiling', (req, res) => {
  const sql = `select adopts.adopt_id,adopts.adopt_time,adopts.true_name,adopts.phone,adopts.address,pets.pet_id,pets.image,pets.petname,pets.gender,pets.breed,pets.age,pets.health,pets.briefing,pets.sterilization,pets.region from adopts join pets on adopts.pet_id=pets.pet_id where adopts.adopt_status='领养成功' order by adopt_id desc`
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('待领养宠物管理接口错误', err.message)
    } else {
      res.send(data)
      console.log('待领养宠物管理接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})
//获取待救助信息
getRouter.get('/jiuzhu', (req, res) => {
  const sql = `select * from helps order by help_id desc`
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('待救助信息获取接口错误', err.message)
    } else {
      res.send(data)
      console.log('待救助信息获取接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})

//获取评论列表
getRouter.get('/pinlun', (req, res) => {
  const sql = `select reviews.username,reviews.forum_id,reviews.review,reviews.review_time,reviews.review_id,users.head_img from reviews join users on reviews.username=users.username order by review_id desc `
  db.query(sql, (err, data) => {
    if (err) {
      return console.log('评论列表接口错误', err.message)
    } else {
      res.send(data)
      console.log('评论列表接口访问成功', date_fns())
      console.log('-------------------------------------')
    }
  })
})

module.exports = {
  getRouter,
}
